package models

import (
	"github.com/beego/beego/v2/client/orm"
)

type RefundsData struct {
	Id            int64
	Refunds       *Refunds        `orm:"rel(fk);null;on_delete(do_nothing)"`
	Products      *ErpProducts    `orm:"rel(fk);null;on_delete(do_nothing)"`
	Inventory     *ErpInventory   `orm:"rel(fk);null;on_delete(do_nothing)"`
	Snd           *ErpProductsSnd `orm:"rel(fk);null;on_delete(do_nothing)"`
	RetailsDataId int64
	Amount        int64   //产品数量
	OriginalPrice float64 //产品原价
	DiscountRate  float64 //产品折扣率
	UnitPrice     float64 //产品折后单价
	TotalPrice    float64 //折后单价*产品数量
}

func RefundsDataTableName() string {
	return TableName("sa_refunds_data")
}

func (a *RefundsData) TableName() string {
	return RefundsDataTableName()
}

type RefundsDataQueryParam struct {
	BaseQueryParam
	RefundsId int64
}

//查询列表数据
func RefundsDataList(params *RefundsDataQueryParam) ([]*RefundsData, int64) {

	query := orm.NewOrm().QueryTable(RefundsDataTableName()).RelatedSel("Products", "Inventory", "Snd", "Inventory__Acceptance")

	data := make([]*RefundsData, 0)

	if params.RefundsId > 0 {
		query = query.Filter("refunds_id", params.RefundsId)
	}
	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}

	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&data)

	return data, total
}
